<template>
    <div class="dashboard-container">
        <div class="search">
            <el-form ref="form" :model="params" label-position="top" size="medium">
                <el-row :gutter="20">
                    <el-col :span="8">
                        <el-form-item label="城市">
                            <el-input placeholder="请输入城市名称" v-model="params.city" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="法拍资产类型">
                            <el-select 
                                filterable 
                                clearable
                                v-model="params.catagory" 
                                placeholder="请选择" 
                                style="width: 100%;"
                            >
                                <el-option v-for="item,index in catagoryList" :key="index" :label="item" :value="item"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="抓取时间">
                            <el-date-picker
                                v-model="createTime"
                                type="datetimerange"
                                range-separator="至"
                                start-placeholder="开始日期"
                                end-placeholder="结束日期"
                                @change="handleChangeTime()"
                                style="width: 100%;"
                            >
                            </el-date-picker>
                        </el-form-item>
                    </el-col>  
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="8">
                        <el-form-item label="成交时间">
                            <el-date-picker
                                v-model="tradeTime"
                                type="datetimerange"
                                range-separator="至"
                                start-placeholder="开始日期"
                                end-placeholder="结束日期"
                                @change="handleChangeTime()"
                                style="width: 100%;"
                            >
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </div>
        <div class="opt">
            <el-button type="primary" plain @click="handleSearch">
                <i class="el-icon-search"></i>
                查询
            </el-button>
            <el-button type="primary" plain @click="visibleDialog=true" v-loading="loadingExport">
                导出
            </el-button>
            
        </div>
        <!-- 内容栏 -->
      <div class="body">

        <!-- 列表 -->
        <el-table :data="list" v-loading="loading">
          <el-table-column label="ID" prop="id"></el-table-column>
          <el-table-column label="区域">
            <template slot-scope="{row}">
                {{ row.province + row.city }}
            </template>
          </el-table-column>
          <el-table-column label="资源名称" prop="name"></el-table-column>
          <el-table-column label="类型" prop="catagory"></el-table-column>
          <!-- <el-table-column label="经纬度">
            <template slot-scope="{row}">
                {{ row.longitude + ',' +  row.latitude }}
            </template>
          </el-table-column> -->
          
          <!-- <el-table-column label="交易价格" prop="tradePrice"></el-table-column> -->
          <el-table-column label="交易阶段" prop="tradePhase"></el-table-column>
          <el-table-column label="交易结果" prop="tradeResult"></el-table-column>
          <el-table-column label="更新时间" prop="updateTime"></el-table-column>
          <el-table-column label="成交时间" prop="tradeTime"></el-table-column>
          <el-table-column label="状态" prop="status"></el-table-column>
          <el-table-column label="源">
            <template slot-scope="{row}">
                <a :href="row.linkUrl" target="_blank" style="color: green;">跳转</a>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="page">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="params.pageIndex+1"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="params.pageCount"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
          </el-pagination>
        </div>

      </div>

      <!-- 导出类型选择 -->
      <el-dialog 
        title="导出" 
        :visible.sync="visibleDialog" 
        width="600px"
        @close="visibleDialog=false" 
        :close-on-click-modal="false"
    >
            <el-form >
                <el-form-item label="导出资产格式"  required>
                    <el-select clearable filterable v-model="propertyType" placeholder="请选择" style="width: 100%;">
                        <el-option v-for="item in TARGET_TYPE_LIST" :key="item.value" :label="item.label" :value="item.value"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div>
                <el-button type="primary" @click="handleExport" v-loading="loadingExport">导出</el-button>
                <el-button @click="visibleDialog = false">关闭</el-button>
            </div>
      </el-dialog>

    </div>
</template>
<script>
import { crawlerGetListTb } from '@/api/crawler'
import { API_BASE_URL, TARGET_TYPE_LIST } from '@/utils/common'
import {  transFormValue } from '@/utils/tools'
import moment from 'moment'
import axios from 'axios'
  
export default {
    data(){
      return {
        API_BASE_URL,
        TARGET_TYPE_LIST,
        list: [],
        loading: false,
        total: 0,
        params: {
            pageCount: 10,
            pageIndex: 0,
            city: '',
            catagory: '', //资产类型, 可选项有: ‘土地’,’住宅用房’,’商业用房’,’工业用房’,’其他用房’
            destPropertyType: '', //对应的资产类型，-1表示未设置过该类型的数据
            maxUpdateTime: '',  //最大抓取时间
            minUpdateTime: '', //最小抓取时间
            minTradeTime:'',
            maxTradeTime: ''
        },
        catagoryList: [ '土地','住宅用房','商业用房','工业用房','其他用房'],
        loadingExport: false,
        visibleDialog: false,
        propertyType: 102,
        createTime: null,
        tradeTime: null
      }
    },
    mounted(){
      this.handleSearch();
    },
  
    methods:{
    //修改创建时间区间
        handleChangeTime(){
            if(this.createTime){
                this.params.minUpdateTime = moment(this.createTime[0]).valueOf();
                this.params.maxUpdateTime = moment(this.createTime[1]).valueOf();
            }else{
                this.params.minUpdateTime = this.params.maxUpdateTime = '';
            }
            if(this.tradeTime){
                this.params.minTradeTime = moment(this.tradeTime[0]).valueOf();
                this.params.maxTradeTime = moment(this.tradeTime[1]).valueOf();
            }else{
                this.params.minTradeTime = this.params.maxTradeTime = '';
            }
        },
        //导出文件
        handleExport(){
            this.loadingExport = true;
            let { city, catagory, maxUpdateTime, minUpdateTime, minTradeTime, maxTradeTime } = this.params
            let exportParams = {
                city, catagory, maxUpdateTime, minUpdateTime, propertyType: this.propertyType, minTradeTime, maxTradeTime 
            }
            axios({
                url: `${API_BASE_URL}/crawler/exportTb${transFormValue(exportParams)}`,
                method: 'POST',
                responseType: 'blob'
            }).then((res) => {
                const blob = res.data
                const fileName = moment().format('YYYY-MM-DD') +'阿里法拍导出.xlsx'
                const elink = document.createElement('a')
                elink.download = fileName
                elink.style.display = 'none'
                elink.href = window.URL.createObjectURL(blob)
                document.body.appendChild(elink)
                elink.click()
                window.URL.revokeObjectURL(elink.href)// 释放对象
                document.body.removeChild(elink)// 移除元素

                
            }).finally(()=>{
                console.log('结束了')
                this.loadingExport = false;
                this.dialogVisibleExport = false;
            })
        },
        //修改搜索条件
        handleSearch(){
            this.params.pageIndex = 0;
            this.getData();
        },
        // 搜索数据
        async getData(){
            this.loading = true;
            let data = await crawlerGetListTb(this.params)
            this.loading = false;
            this.list = data.list.map(item=>{
                item.updateTime = moment(item.updateTime).format('YYYY-MM-DD HH:mm:ss')
                item.tradeTime = moment(item.tradeTime).format('YYYY-MM-DD HH:mm:ss')
                return item;
            })
            this.total = data.totalCount
        },
        // 修改每页数量
        handleSizeChange(val){
            this.params.pageIndex = 0;
            this.params.pageCount = val;
            this.getData();
        },
        // 切换分页
        handleCurrentChange(val){
            this.params.pageIndex = val - 1;
            this.getData();
        }
    }
}
</script>

<style lang="scss" scoped>
.dashboard {
    &-container {
        margin: 30px;
        position: relative;
        .opt{
            margin-bottom: 10px;
        }
        .right {
            position: absolute;
            right: 0;
            top: 0;
        }

        .page {
            padding: 20px 0;
            text-align: center;
        }
    }

    &-text {
        font-size: 30px;
        line-height: 46px;
    }
}
</style>